package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbItemCat;
import com.dongdongshop.pojo.seller.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemcat")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;
    @Reference
    private TemplateService templateService;

    private final Logger logger= LoggerFactory.getLogger(ItemCatController.class);

    @RequestMapping("toList")
    public String toList(Model model){
        List<TbTypeTemplate> templates=templateService.findAll();
        model.addAttribute("templates",templates);
        return "admin/item_cat";
    }

    @RequestMapping("listItemCat")
    @ResponseBody
    public Result listItemCat(@RequestParam(defaultValue = "0") Long parentId){
        //System.out.println(parentId+"一-------------");
        try {
            List<TbItemCat> list=itemCatService.listItemCat(parentId);
          //  logger.info("查询成功"+list);
            if (list.size() <=0){
                return Result.response(ResultEnum.FAIL).setData("没有数据");
            }
            return Result.response(ResultEnum.OK).setData(list);
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("getDataById/{id}")
    @ResponseBody
    public Result getDataById(@PathVariable("id")Long id){
        try {
            if (id==0){
                return Result.response(ResultEnum.OK).setData("顶级分类列表");
            }else {
                TbItemCat tbItemCat=itemCatService.getDataById(id);
                return Result.response(ResultEnum.OK).setData(tbItemCat);
            }
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }


    //删除
    @RequestMapping("deleteIds")
    @ResponseBody
    public Result deleteIds(@RequestParam("ids[]")Long[] ids){
        try {
            int i=itemCatService.deleteIds(ids);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //增加
    @RequestMapping("addItemCat")
    @ResponseBody
    public Result addItemCat(TbItemCat tbItemCat){
        try {
            itemCatService.addItemCat(tbItemCat);
            return Result.response(ResultEnum.OK);
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    //回显
    @RequestMapping("updateById")
    @ResponseBody
    public Result updateById(@RequestParam("id")Long id){
        try {
            TbItemCat tbItemCat=itemCatService.updateById(id);
            return Result.response(ResultEnum.OK).setData(tbItemCat);
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    //修改
    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(TbItemCat tbItemCat){
        try {
            int i=itemCatService.updateItemCat(tbItemCat);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

}
